package cn.b.sky.user.filter;


/**
 * 
 * <strong>Title : UrlPatternMatcher.java<br></strong>
 * <strong>Package : cn.b.sky.user.filter<br></strong>
 * <strong>Description : </strong>拦截的url适配器<br> 
 * <strong>Create on : 2016-7-18 上午9:24:34<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public abstract class UrlPatternMatcher {
  
  public static final UrlPatternMatcher DEFAULT_MATCHER = new AllPassUrlPatternMatcher();

  private String urlPattern;

  /**
   * 
   * <strong>Title : create<br></strong>
   * <strong>Description : </strong>工厂方法创建url适配类<br> 
   * <strong>Create on : 2016-7-18 上午9:33:58<br></strong>
   * <p>
   * @param urlPattern
   * @return
   * UrlPatternMatcher
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static UrlPatternMatcher create(String urlPattern) {
    UrlPatternMatcher urlPatternMatcher;
    if ("/*".equals(urlPattern)) {
      urlPatternMatcher = DEFAULT_MATCHER;
    } else {
      if (urlPattern.startsWith("*")) {
        String suffix = urlPattern.substring(1);

        urlPatternMatcher = new SuffixUrlPatternMatcher(suffix);
      } else {
        if (urlPattern.endsWith("*")) {
          String prefix = urlPattern.substring(0, urlPattern.length() - 1);

          urlPatternMatcher = new PrefixUrlPatternMatcher(prefix);
        } else {
          urlPatternMatcher = new EqualsUrlPatternMatcher(urlPattern);
        }
      }
    }
    urlPatternMatcher.setUrlPattern(urlPattern);

    return urlPatternMatcher;
  }

  public abstract boolean matches(String paramString);

  public boolean shouldRedirect(String url) {
    url = null;
    return false;
  }

  public String getUrlPattern() {
    return this.urlPattern;
  }

  public void setUrlPattern(String urlPattern) {
    this.urlPattern = urlPattern;
  }

  /**
   * 
   * <strong>Title : UrlPatternMatcher.java<br></strong>
   * <strong>Package : cn.b.sky.user.filter<br></strong>
   * <strong>Description : </strong>全路径<br> 
   * <strong>Create on : 2016-7-18 上午9:34:54<br></strong>
   * <p>
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人|修改日期|修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  static class AllPassUrlPatternMatcher extends UrlPatternMatcher {
    @Override
    public boolean matches(String url) {
      return true;
    }
  }

  static class EqualsUrlPatternMatcher extends UrlPatternMatcher {
    private final String urlPattern;

    public EqualsUrlPatternMatcher(String urlPattern) {
      this.urlPattern = urlPattern;
    }

    @Override
    public boolean matches(String url) {
      return url.equals(this.urlPattern);
    }
  }

  static class PrefixUrlPatternMatcher extends UrlPatternMatcher {
    private String withoutSlash;

    private final String prefix;

    public PrefixUrlPatternMatcher(String prefix) {
      this.prefix = prefix;

      if (prefix.endsWith("/")) {
        this.withoutSlash = prefix.substring(0, prefix.length() - 1);
      } else {
        this.withoutSlash = prefix;
      }
    }

    @Override
    public boolean matches(String url) {
      return (shouldRedirect(url)) || (url.startsWith(this.prefix));
    }

    @Override
    public boolean shouldRedirect(String url) {
      return url.equals(this.withoutSlash);
    }
  }

  static class SuffixUrlPatternMatcher extends UrlPatternMatcher {
    private final String suffix;

    public SuffixUrlPatternMatcher(String suffix) {
      this.suffix = suffix;
    }

    @Override
    public boolean matches(String url) {
      return url.endsWith(this.suffix);
    }
  }
}
